var postsData = require('../../../n_data/n_data.js')
var app = getApp();
Page({
  data: {
    title:""
  },
  onLoad: function (option) {
    var postId = option.id;
    this.data.currentPostId = postId;
    var postData = postsData.postList[postId];
    this.setData({
      postData: postData
    })

    var postsCollected = wx.getStorageSync('posts_collected')
    if (postsCollected) {
      var postCollected = postsCollected[postId]
      if (postCollected) {
        this.setData({
          collected: postCollected
        })
      }
    }
    else {
      var postsCollected = {};
      postsCollected[postId] = false;
      wx.setStorageSync('posts_collected', postsCollected);
    }
  },

  onColletionTap: function (event) {
    // this.getPostsCollectedSyc();
    this.getPostsCollectedAsy();
  },

  getPostsCollectedAsy: function () {
    var that = this;
    wx.getStorage({
      key: "posts_collected",
      success: function (res) {
        var postsCollected = res.data;
        var postCollected = postsCollected[that.data.currentPostId];
        postCollected = !postCollected;
        postsCollected[that.data.currentPostId] = postCollected;

        that.showToast(postsCollected, postCollected);
      }
    });

  },


  showToast: function (postsCollected, postCollected) {
    wx.setStorageSync('posts_collected', postsCollected);
  
    this.setData({
      collected: postCollected
    })
    wx.showToast({
      title: postCollected ? "稍后再看" : "取消成功",
      duration: 1000,
      icon: "success"
    })
  },

  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '',
      path: '/page/user?id=123',//某用户
      success: function (res) {
        wx.showToast({
          title: "转发成功",
          duration: 1000,
          icon: "success"
        })
      },
      fail: function (res) {
        
      }
    }
  }

})